/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemadecompras.interfaz;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import sistemadecompras.negocio.Factura;
import sistemadecompras.negocio.Facturas;

/**
 *
 * @author Jorge
 */
public class FacturasPorCliente extends javax.swing.JFrame {
   
Facturas listaFacturas = new Facturas();
    public FacturasPorCliente() {
        initComponents();
        setDefaultCloseOperation(0); 
    }
    public FacturasPorCliente(String cedula, String nombrecompleto) {
        initComponents();
        actualizatabla(cedula,nombrecompleto);
        setDefaultCloseOperation(0); 
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuFacturaDetallada = new javax.swing.JMenuItem();
        jScrollPane6 = new javax.swing.JScrollPane();
        tblFacturas = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        TXTCLIENTE = new javax.swing.JTextField();

        jMenuFacturaDetallada.setText("IR A DETALLES DE FACTURA");
        jMenuFacturaDetallada.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuFacturaDetalladaMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jMenuFacturaDetalladaMouseReleased(evt);
            }
        });
        jMenuFacturaDetallada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuFacturaDetalladaActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuFacturaDetallada);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblFacturas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero Factura", "Fecha", "Cliente", "Total Pagar"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblFacturas.setComponentPopupMenu(jPopupMenu1);
        tblFacturas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblFacturasMouseClicked(evt);
            }
        });
        jScrollPane6.setViewportView(tblFacturas);

        jLabel1.setText("FACTURAS DEL CLIENTE:");

        TXTCLIENTE.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(TXTCLIENTE, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(26, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TXTCLIENTE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(250, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(0, 43, Short.MAX_VALUE)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblFacturasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFacturasMouseClicked

        // TODO add your handling code here:
    }//GEN-LAST:event_tblFacturasMouseClicked

    private void jMenuFacturaDetalladaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuFacturaDetalladaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuFacturaDetalladaActionPerformed

    private void jMenuFacturaDetalladaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuFacturaDetalladaMouseClicked
    
    }//GEN-LAST:event_jMenuFacturaDetalladaMouseClicked

    private void jMenuFacturaDetalladaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuFacturaDetalladaMouseReleased
      String factura=tblFacturas.getValueAt(tblFacturas.getSelectedRow(), 0).toString();
       FacturasDetalladas f=new FacturasDetalladas(Integer.parseInt(factura));
       f.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuFacturaDetalladaMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FacturasPorCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FacturasPorCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FacturasPorCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FacturasPorCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FacturasPorCliente().setVisible(true);
            }
        });
    }
    public void actualizatabla(String ced, String nombre){
        TXTCLIENTE.setText(nombre); //AQUI SETEO EL NOMBRE DEL CLIENTE SELECCIONADO
        
        listaFacturas.listarFacturasxCliente(ced);
        ArrayList facturas = new ArrayList();
        facturas = listaFacturas.getFacturas();
        DefaultTableModel temp = (DefaultTableModel)  tblFacturas.getModel();
     
        for (int i = tblFacturas.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);}
        for (int i = 0; i < facturas.size(); i++) {
            Factura otraFactura = new Factura();
            otraFactura = (Factura) facturas.get(i);
            Object nuevo[] = {otraFactura.getId_fact(), otraFactura.getFecha(), otraFactura.getId_cliente()}; //esto es por las tres columnas aunque puede variar
            temp.addRow(nuevo);
        }
        tblFacturas.setModel(temp);
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField TXTCLIENTE;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem jMenuFacturaDetallada;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable tblFacturas;
    // End of variables declaration//GEN-END:variables
}
